package com.example.routerlib

import android.app.Activity
import android.app.Application
import android.content.Context
import android.net.Uri
import com.alibaba.android.arouter.facade.callback.NavigationCallback
import com.alibaba.android.arouter.launcher.ARouter
import com.alibaba.android.arouter.utils.TextUtils
import org.w3c.dom.Text

class RouterJump {

    val debug = true

    companion object {
        val getInstance by lazy {
            RouterJump()
        }
    }


    fun init(app: Application) {
        if (debug) {
            ARouter.openLog()
            ARouter.openDebug()
        }
        ARouter.init(app)
    }

    fun open(
        context: Context,
        url: String,
        requestCode: Int? = null,
        callBack: NavigationCallback? = null
    ) {
        if (context != null && !TextUtils.isEmpty(url)) {
            val uri = Uri.parse(url);
            val scheme = uri.scheme
            val host = uri.host
            val path = uri.path

            if (!TextUtils.isEmpty(scheme) && !TextUtils.isEmpty(host) && !TextUtils.isEmpty(path)) {
                val postcard = ARouter.getInstance().build(uri)
                if (requestCode != null && callBack != null) {
                    postcard.navigation(context as Activity, requestCode, callBack)
                } else if (requestCode != null) {
                    postcard.navigation(context as Activity, requestCode)
                } else if (callBack != null) {
                    postcard.navigation(context as Activity, callBack)
                }
            }
        }
    }

    fun getNavigation(path:String?):Any?{
        return ARouter.getInstance().build(path).navigation()
    }

}